package yun.web.activerecord.ext;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.serializer.JSONSerializer;
import com.alibaba.fastjson.serializer.ObjectSerializer;
import org.postgresql.util.PGobject;

import java.io.IOException;
import java.lang.reflect.Type;

public class PGObjectSerializer implements ObjectSerializer {
    public static PGObjectSerializer me = new PGObjectSerializer();

    @Override
    public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) throws IOException {
        PGobject o = (PGobject) object;
        String type = o.getType();
        if ("jsonb".equals(type)|"json".equals(type)) {
            JSONObject jsonObject = JSON.parseObject(o.getValue());
            serializer.write(jsonObject);
        }else{
            serializer.write("");
        }
    }
}
